﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IHISCETeim.BLL;

using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using NPOI.SS.UserModel;
using IHISCETeim.Model;
using NPOI.HSSF.Util;

namespace IHISCETeim.Web.Export.XZModel
{
    public partial class ExportXZJobTrain : ExportPageBase
    {
        protected override string xlsTemplateFileName
        {
            get
            {
                return "行政_岗位培训模板.xls";
            }
        }
        protected override string SheetName
        {
            get
            {
                return "Sheet1";
            }
        }
        /// <summary>
        /// 要导出的岗位ID
        /// </summary>
        protected int JobId
        {
            get
            {
                return GetRequestValue<int>("JobId", 0);
            }
        }
        /// <summary>
        /// 入职员工
        /// </summary>
        protected string UserName
        {
            get
            { 
                return GetRequestValue<string>("UserName","");
            }
        }

        /// <summary>
        /// 入职时间
        /// </summary>
        protected string FromDate
        {
            get
            { 
                return GetRequestValue<string>("FromDate","");
            }
        }


        private XZJobListInfo _XZJobListObj = null;
        protected XZJobListInfo XZJobListObj
        {
            get
            {
                if (_XZJobListObj == null)
                    _XZJobListObj = XZJobTrainController.GetJobByJobId(JobId);
                return _XZJobListObj;
            }
        }

        private DataTable _CurrJobTrainDetail = null;
        /// <summary>
        /// 当前岗位已有的培训内容
        /// </summary>
        protected DataTable CurrJobTrainDetail
        {
            get
            {
                if (_CurrJobTrainDetail == null)
                    _CurrJobTrainDetail = XZJobTrainController.GetAllByJobId2(JobId);
                return _CurrJobTrainDetail;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
           // DataTableCollection dts = UReturnVisitController.GetVisitUsabilityReport(CreateDateFrom, CreateDateTo);
            DataTable exportDT = CurrJobTrainDetail;

            InitializeWorkbook();

            ISheet sheet1 = hssfworkbook.GetSheet(SheetName);
            //
            sheet1.GetRow(0).GetCell(0).SetCellValue(string.Format("{0}({1})入职培训内容", XZJobListObj.JobDept, XZJobListObj.Title));
            sheet1.GetRow(1).GetCell(1).SetCellValue(UserName);
            sheet1.GetRow(1).GetCell(4).SetCellValue(FromDate);    
            //FOR写入
            int rowindexBase = 3;
            int rowindex = 0;
            int rowReal = rowindex + rowindexBase;
            //
            ICellStyle blackBorder = hssfworkbook.CreateCellStyle();
            blackBorder.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;
            blackBorder.BorderLeft = NPOI.SS.UserModel.BorderStyle.THIN;
            blackBorder.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN;
            blackBorder.BorderTop = NPOI.SS.UserModel.BorderStyle.THIN;
            blackBorder.BottomBorderColor = HSSFColor.BLACK.index;
            blackBorder.LeftBorderColor = HSSFColor.BLACK.index;
            blackBorder.RightBorderColor = HSSFColor.BLACK.index;
            blackBorder.TopBorderColor = HSSFColor.BLACK.index;
            foreach (DataRow dr in exportDT.Rows)
            {
                HSSFRow row1 = (HSSFRow)sheet1.CreateRow(rowReal);
                // row1.CreateCell(0).SetCellValue(1);
                ICell cell = row1.CreateCell(0);
                cell.CellStyle = blackBorder;

                cell = row1.CreateCell(1);
                cell.CellStyle = blackBorder;

                cell = row1.CreateCell(2);
                cell.SetCellValue(dr["TrainDept"].ToString());
                cell.CellStyle = blackBorder;


                cell = row1.CreateCell(3);
                cell.SetCellValue(dr["TrainContent"].ToString());
                cell.CellStyle = blackBorder;

                cell = row1.CreateCell(4);
                cell.SetCellValue(dr["ClassHour"].ToString() + dr["Unit"].ToString());
                cell.CellStyle = blackBorder;

                cell = row1.CreateCell(5);
                cell.CellStyle = blackBorder;

                //
                rowindex++;
                rowReal = rowindex + rowindexBase;
            }          
            sheet1.ForceFormulaRecalculation = true;
            //批量设置边框
           

            //for (int iRow = 3; iRow < rowReal; iRow++)
            //{
            //    IRow row = sheet1.GetRow(iRow);
            //    for (int iCol = 0; iCol <=5; iCol++)
            //    {
            //        //the first cell of each row * the first cell of each column
            //      //  string formula = GetCellPosition(iRow, 0) + "*" + GetCellPosition(0, iCol);
            //        ICell cell = row.GetCell(iCol);
            //       // cell.CellFormula = formula;
            //        //set the cellstyle to the cell
            //        cell.CellStyle = blackBorder;
            //    }
            //}


            WriteToFile();
        }

        protected override string GetXlsFileName(string oldFileName)
        {
            //return base.GetXlsFileName(oldFileName);

            return string.Format("{0}_{1}_{2}.xls", XZJobListObj.JobDept, XZJobListObj.Title,DateTime.Now.ToString("yyyyMMdd")).Trim();
        }
    }
}